package com.chebaohui.iqs.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.Paramter;
import com.chebaohui.iqs.entity.Result;
import com.chebaohui.iqs.entity.base.Pay;
import com.chebaohui.iqs.entity.pay.Pays;
import com.chebaohui.iqs.service.PayService;
import com.chebaohui.iqs.sign.SignParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * describe:控制层:支付
 * 
 * @author regas_xie
 *
 */
@Api(value = "pay", description = "支付接口", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping(value = "pay")
public class PayController {
	/**
	 * log:日志输出
	 */
	private Log log = LogFactory.getLog(PayController.class);
	@Autowired
	private PayService payService;

	@ApiOperation(value = "微信服务器配置", notes = "微信服务器配置（暂未启用）", hidden = true)
	@RequestMapping(value = "/wx/sign", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String callback(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String[]> map = request.getParameterMap();
		log.info("微信服务器配置:" + JSONObject.toJSONString(map));
		if (null == map.get("echostr"))
			return "hello";
		else
			return map.get("echostr")[0];
	}

	@ApiOperation(value = "错误页面", httpMethod = "GET", notes = "银联、微信支付发起", hidden = true)
	@RequestMapping(value = "/error", method = RequestMethod.GET)
	public ModelAndView errorView(
			@ApiParam(value = "类型", required = true) @RequestParam(value = "type", required = false) int type) {
		return payService.errorView(type);
	}

	@ApiOperation(value = "微信公众号支付", httpMethod = "GET", notes = "微信公众号支付-->返回页面;注意只能微信内置浏览器访问有效", hidden = true)
	@RequestMapping(value = "/wx", method = RequestMethod.GET)
	public void wxPay(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "订单号", required = true) @RequestParam(value = "orderSid", required = false) String orderSid) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		try {
			response.sendRedirect(payService.wxPay(orderSid, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ApiOperation(value = "微信授权回调", notes = "微信授权回调", hidden = true)
	@RequestMapping(value = "/wx/callBack", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView callBack(HttpServletRequest request, HttpServletResponse response) {

		return payService.backToPrice(request);
	}

	@ApiOperation(value = "微信支付回调", notes = "微信支付回调", hidden = true)
	@RequestMapping(value = "/wx/priceBack", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void priceBack(HttpServletRequest request, HttpServletResponse response) {
		try {
			payService.priceBack(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@ApiOperation(value = "支付发起", httpMethod = "POST", notes = "银联、微信支付发起,返回支付地址url")
	@RequestMapping(value = "/originate", method = RequestMethod.POST)
	public Result<Pay> priceUrlPay(
			@ApiParam(value = "支付请求参数", required = true) @RequestBody(required = false) Paramter<Pays> param) {
		Result<Pay> result = null;

		if (!SignParam.decrypted(param.getSign()))
			result = new Result<>(ResultCode.L01);

		result = payService.urlPay(param);
		log.info("支付发起:" + "\n" + "请求参数：" + JSONObject.toJSONString(param) + "\n" + "返回结果："
				+ JSONObject.toJSONString(result));
		return result;
	}

}
